<style>
  .layui-form-label {
    width: 90px;
  }

  .layui-input-block {
    margin-left: 120px;
  }

  #erpPurchaseRequisitions-view .layui-disabled {
    color: #393d49 !important;
    background-color: #fff;
  }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form lay-header" >
        <div class="toolbar">
          <div class="layui-form-item lay-query-content" id="xxxxx">
            <div class="lay-query-box">
              <div class="query-lt">
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">退/换货类型：</label>
                  <div class="layui-input-inline mr0">
                    <select name="type" lay-filter="type" id="supportReturntype"></select>
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">客户名称：</label>
                  <div class="layui-input-inline mr0">
                    <input name="customerName" class="layui-input" type="text" placeholder="请输入客户名称" />
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">记录编码：</label>
                  <div class="layui-input-inline mr0">
                    <input name="recordCode" class="layui-input" type="text" placeholder="请输入记录编码" />
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">订单编码：</label>
                  <div class="layui-input-inline mr0">
                    <input name="orderCode" class="layui-input" type="text" placeholder="请输入订单编码" />
                  </div>
                </div>
                <!-- <div class="layui-inline">
                  <label class="layui-form-label w-auto">时间范围：</label>
                  <div class="layui-input-inline mr0">
                    <input name="dateRange" class="layui-input" type="text" placeholder="请选择时间范围" id="dateRange" />
                  </div>
                </div> -->
              </div>
              <div class="query-rt">
                <div class="layui-inline">
                  <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                  <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <button id="addFormBtn" class="layui-btn icon-btn color-reseda">添加</button>
            </div>
            <div class="lay-btn-rt"></div>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <table class="layui-table" id="returnRecordsTable" lay-filter="returnRecordsTable"></table>
    </div>
  </div>
</div>
<!-- 状态1 待审核 2 待收货 3 已收货 4 待发货 5 已发货 6 完结 -->
<!-- 表格操作列 -->
<script type="text/html" id="returnRecordsTable-bar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
  <!-- {{# if(d.status == "1"){ }} -->
  <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
  <a class="layui-btn layui-btn layui-btn-xs" lay-event="audit">审核</a>
  <!-- {{# } else { }} -->
  <!-- {{# if(d.status == "2"){ }} -->
  <a class="layui-btn layui-btn layui-btn-xs" lay-event="getData">收货</a>
  <!-- {{# } }} -->
  <a class="layui-btn layui-btn layui-btn-xs" lay-event="getDataHis">收货记录</a>
  <!-- {{# if(d.typeNameData=="换货"&&d.status == "4"){ }} -->
  <a class="layui-btn layui-btn layui-btn-xs" lay-event="setData">发货</a>
  <a class="layui-btn layui-btn layui-btn-xs" lay-event="setDataHis">发货记录</a>
  <!-- {{# } }} -->
  <!-- {{# } }} -->

  <!-- {{# if(d.status == "3"&&d.typeNameData=="换货"){ }} -->
  <!-- {{# } }} -->

  <!-- {{# if(d.status == "stayConfirm"){ }} -->
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  <!-- {{# } }} -->
</script>

<script type="text/html" id="erpInventoryList">
  <div class="layui-card">
    <div class="layui-card-body">
      <div class="layui-form toolbar">
        <div class="layui-form-item">
          <div class="layui-inline">
            <div class="layui-input-inline mr0">
              <input id="erpInventoryList-code" class="layui-input" type="text" placeholder="输入物料编号" />
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-input-inline mr0">
              <input id="erpInventoryList-name" class="layui-input" type="text" placeholder="输入物料名称" />
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-input-inline mr0">
              <select id="erpInventoryList-way">
                <option value="">请选择计价方式</option>
                <option value="piece">按件</option>
                <option value="weight">按重量</option>
              </select>
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-input-inline mr0">
              <input name="productCategoryId" type="hidden" lay-verify="required" />
              <div class="layui-input-inline mr0" id="productCategoryId"></div>
            </div>
          </div>
          <div class="layui-inline">
            <button id="erpInventoryList-btn-search" class="layui-btn icon-btn">
              <i class="layui-icon">&#xe615;</i>
              搜索
            </button>
            <button id="erpInventoryList-btn-choose" class="layui-btn layui-btn-normal">确认选择</button>
          </div>
        </div>
      </div>
      <table class="layui-table" id="erpInventoryList-table" lay-filter="erpInventoryList-table"></table>
    </div>
  </div>
</script>

<!-- js部分 -->

<script>
  layui.use(['layer', 'form', 'table', 'admin', 'laytpl', 'laydate', 'util', 'formSelects'], async function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var upload = layui.upload;
    var laytpl = layui.laytpl;
    var laydate = layui.laydate;
    var util = layui.util;
    var formSelects = layui.formSelects;
    let typeObj = {};
    // $.get('components/component/queryBox.html', function (tpl) {
    //   var data = { name: 'Example', description: 'This is a description.' };
    //   data.fields = [
    //     { label: '退/换货类型', type: 'select', name: 'type', id: 'supportReturntype' },
    //     { label: '客户名称', type: 'text', name: 'customerName', placeholder: '请输入客户名称' },
    //     { label: '记录编码', type: 'text', name: 'recordCode', placeholder: '请输入记录编码' },
    //     { label: '订单编码', type: 'text', name: 'orderCode', placeholder: '请输入订单编码' },
    //     { label: '时间范围', type: 'text', name: 'dateRange', placeholder: '请选择时间范围', id: 'dateRange' },
    //   ];
    //   data.formName='zzq'
    //   data.tableName='returnRecordsTable'
    //   laytpl(tpl).render(data, function (html) {
    //     $('#xxxxx').html(html);
    //   });
    // });

 
    $(function () {
      if ($('#exchangeRecords').length) {
        typeObj = {
          typeNameData: '换货',
          addUrl: 'enterprise/sell/admin/barterGoodsRecords/add',
          listUrl: 'enterprise/sell/admin/barterGoodsRecords/list',
          editUrl: 'enterprise/sell/admin/barterGoodsRecords/update',
          detailUrl: 'enterprise/sell/admin/barterGoodsRecords/detail/',
          auditUrl: 'enterprise/sell/admin/barterGoodsRecords/audit/',
          addLogisticsInfo: 'enterprise/sell/admin/barterGoodsRecords/addLogisticsInfo',
        };
      } else {
        typeObj = {
          typeNameData: '退货',
          addUrl: 'enterprise/sell/admin/returnGoodsRecords/add',
          listUrl: 'enterprise/sell/admin/returnGoodsRecords/list',
          editUrl: 'enterprise/sell/admin/returnGoodsRecords/update',
          detailUrl: 'enterprise/sell/admin/returnGoodsRecords/detail/',
          auditUrl: 'enterprise/sell/admin/returnGoodsRecords/audit/',
          addLogisticsInfo: 'enterprise/sell/admin/returnGoodsRecords/addLogisticsInfo',
        };
      }
    });
    //时间范围
    laydate.render({
      elem: '#pTime',
      type: 'date',
      range: true,
      done: function (value, date, endDate) {
        if (value) {
          var dateValue = value.split(' - ');
          $('#sTime').val(dateValue[0] + ' 00:00:00');
          $('#eTime').val(dateValue[1] + ' 23:59:59');
        } else {
          $('#sTime').val('');
          $('#eTime').val('');
        }
      },
    });
    await admin.initDataSelectPro('enterprise/sell/admin/customerReturnType/list', {}, 'supportReturntype', `${typeObj.typeNameData}类型`);

    // 渲染表格
    var insTb = table.render({
      elem: '#returnRecordsTable',
      url: config.base + typeObj.listUrl,
      where: {
        access_token: config.getToken().access_token,
      },
      page: true,
      cellMinWidth: 100,
      cols: [
        [
          { type: 'numbers',title:'序号', height: 50 },
          { field: 'code', align: 'center', title: typeObj.typeNameData + '记录编号' },
          { field: 'typeName', align: 'center', title: typeObj.typeNameData + '类型' },
          { field: 'orderCode', align: 'center', title: '关联订单编号' },
          { field: 'supplierName', align: 'center', title: '客户名称' },
          { field: 'remark', align: 'center', title: typeObj.typeNameData + '备注' },
          { field: 'createTime', align: 'center', title: '提交时间' },
          // { field: 'creatorName', align: 'center', title: typeObj.typeNameData + '人' },
          { field: 'totalMoney', align: 'center', title: typeObj.typeNameData + '金额' },
          { field: 'statusName', align: 'center', title: '状态' },
          {
            align: 'center',
            // toolbar: '#returnRecordsTable-bar',
            title: '操作',
            width: 300,
            templet: function (d) {
              // 在这里将 typeObj 的数据附加到 d 上
              d.typeNameData = typeObj.typeNameData;
              return laytpl($('#returnRecordsTable-bar').html()).render(d);
            },
          },
        ],
      ],
    });
    // 添加按钮点击事件
    $('#addFormBtn').click(function () {
      openForm('add');
    });

    function openForm(eventName, data) {
      const titleObj = {
        add: '添加退货信息',
        edit: '修改退货信息',
        detail: '查看退货详情',
      };
      admin.formOpen({
        title: titleObj[eventName],
        area: ['90%', '90%'],
        offset: '50px',
        path: 'components/sell/returnRecords/returnRecordsForm.html',
        success: function () {
          parentData.data = {
            // ...typeObj,
            ...data,
          };
          parentData.eventName = eventName;
          parentData = { ...parentData, ...typeObj };
        },
      });
    }
    function openDataHisForm(eventName, data) {
      admin.formOpen({
        title: eventName == 'getDataHis' ? '收货记录' : '发货记录',
        area: ['90%', '90%'],
        offset: '50px',
        path: 'components/sell/returnRecords/returnRecordsDataHis.html',
        success: function () {
          parentData.data = {
            // ...typeObj,
            ...data,
          };
          parentData.eventName = eventName;
          parentData = { ...parentData, ...typeObj };
        },
      });
    }
    // 工具条点击事件
    table.on('tool(returnRecordsTable)', function (obj) {
      var data = obj.data;
      var layEvent = obj.event;
      if (layEvent === 'edit') {
        // 修改
        admin.req(typeObj.detailUrl + data.id, {}, (res) => {
          if (res.code === 200) {
            openForm('edit', { ...data, ...res.data, ...typeObj });
          }
        });
      } else if (obj.event === 'del') {
        //删除
        doDelete(obj);
      } else if (obj.event === 'audit') {
        //审核
        layer.confirm('确定要审核吗？', { skin: 'layui-layer-admin' }, function (index) {
          admin.req(
            typeObj.auditUrl + data.id,
            {},
            function (res) {
              if (res.code === 200) {
                layer.close(index);
                layer.msg(res.msg);
                table.reload('returnRecordsTable');
              }
            },
            'POST',
          );
          layer.close(index);
        });
      } else if (obj.event === 'detail') {
        admin.req(typeObj.detailUrl + data.id, {}, (res) => {
          if (res.code === 200) {
            openForm('detail', {
              ...data,
              ...res.data,
              ...typeObj,
            });
          }
        });
      } else if (obj.event == 'getData') {
        admin.req(typeObj.detailUrl + data.id, {}, (res) => {
          if (res.code === 200) {
            openForm('getData', {
              ...data,
              ...res.data,
              ...typeObj,
            });
          }
        });
      } else if (obj.event == 'setData') {
        admin.req(typeObj.detailUrl + data.id, {}, (res) => {
          if (res.code === 200) {
            openForm('setData', {
              ...data,
              ...res.data,
              ...typeObj,
            });
          }
        });
      } else if (obj.event == 'getDataHis') {
        admin.req(typeObj.detailUrl + data.id, {}, (res) => {
          if (res.code === 200) {
            openDataHisForm('getDataHis', {
              ...data,
              ...res.data,
              ...typeObj,
            });
          }
        });
      } else if (obj.event == 'setDataHis') {
        admin.req(typeObj.detailUrl + data.id, {}, (res) => {
          if (res.code === 200) {
            openDataHisForm('setDataHis', {
              ...data,
              ...res.data,
              ...typeObj,
            });
          }
        });
      }
    });

    // 删除
    var doDelete = function (obj) {
      layer.confirm(
        '确定要删除吗？',
        {
          skin: 'layui-layer-admin',
        },
        function (i) {
          layer.close(i);
          layer.load(2);
          admin.req(
            'enterprise/purchase/admin/purchaseRequisitions/' + obj.data.id,
            {},
            function (data) {
              layer.closeAll('loading');
              if (data.code == 200) {
                layer.msg(data.msg, {
                  icon: 1,
                  time: 500,
                });
                obj.del();
              } else {
                layer.msg(data.msg, {
                  icon: 2,
                  time: 1000,
                });
              }
            },
            'DELETE',
          );
        },
      );
    };

    // 搜索
    form.on('submit(formSubSearchLog)', function (data) {
      insTb.reload({ where: data.field, page: { curr: 1 } }, 'data');
    });

    // 重置搜索
    form.on('submit(resets)', function (data) {
      insTb.reload({ where: admin.resetSearch(data.field), page: { curr: 1 } }, 'data');
    });

    // 导出excel
    $('#erpPurchaseRequisitionsBtnExport').click(function () {
      var checkRows = table.checkStatus('returnRecordsTable');
      if (checkRows.data.length == 0) {
        layer.msg('请选择要导出的数据', { icon: 2 });
      } else {
        table.exportFile(insTb.config.id, checkRows.data, 'xls');
      }
    });
  });
</script>
